package dao;
import java.util.List;

import org.hibernate.classic.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import entity.Cliente;
import entity.Funcionario;

public class ClienteDaoImplementation implements ClienteDao {

	public void save(Cliente cliente) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(cliente);
		transaction.commit();
		session.close();
	}

	public void update(Cliente cliente) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(cliente);
		transaction.commit();
		session.close();
	}

	public void delete(Cliente cliente) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(cliente);
		transaction.commit();
		session.close();
	}

	public void consult(Cliente cliente) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		cliente = (Cliente) session.load(Cliente.class, new String("Cliente"));
	}

	public Cliente getCliente(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Cliente) session.load(Cliente.class, id);
	}
	
	public List<Cliente> list() 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "SELECT new Cliente( c.idCliente, c.nome, c.rg, c.cpf, c.telefone, c.email) FROM Cliente c";
        List<Cliente> lista = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return lista;
	}
	
	

}
